// pages/editAddress/editAddress.js
import { areaList } from '@vant/area-data'
import { getAddressDetailApi, addAddressApi, updateAddressApi } from '../../api/address'

Page({
  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    areaList,
    addressId: '',
    areaValue: '',
    addressInfo: {
      province: '',
      provinceCode: '',
      city: '',
      cityCode: '',
      area: '',
      areaCode: '',
      detail: '',
      name: '',
      phone: '',
      isDefault: false
    }
  },

  showArea() {
    this.setData({
      show: true
    })
  },

  getAreaValue() {
    const area = this.data.addressInfo.area
    let areaValue = ''
    for (const key in areaList.county_list) {
      if (Object.hasOwnProperty.call(areaList.county_list, key)) {
        const value = areaList.county_list[key]
        if (value === area) {
          areaValue = key
          break
        }
      }
    }
    this.setData({
      areaValue
    })
  },

  async getAddressDetail() {
    const [err, addressInfo] = await getAddressDetailApi({
      id: this.data.addressId
    })
    if (err) return
    this.setData(
      {
        addressInfo
      },
      () => {
        this.getAreaValue()
      }
    )
  },

  areaConfirm(e) {
    const value = e.detail.values
    this.setData({
      areaValue: value[2].code,
      'addressInfo.province': value[0].name,
      'addressInfo.city': value[1].name,
      'addressInfo.area': value[2].name,
      show: false
    })
  },

  areaCancel() {
    const instance = this.selectComponent('.area')
    instance.reset(this.data.areaValue)
    this.setData({
      show: false
    })
  },

  async addAddress() {
    for (let i = 0; i < ['area', 'detail', 'name', 'phone'].length; i++) {
      const key = ['area', 'detail', 'name', 'phone'][i]
      if ((this.data.addressInfo[key] ?? '') == '') {
        wx.showToast({
          title: '请完善信息',
          icon: 'none'
        })
        return
      }
    }
    const [err] = await addAddressApi(this.data.addressInfo)
    if (err) return false
    wx.showToast({
      title: '添加地址成功',
      icon: 'none'
    })
    wx.navigateBack({
      delta: 0
    })
    return true
  },

  async updateAddress() {
    const [err] = await updateAddressApi({
      ...this.data.addressInfo,
      id: this.data.addressId
    })
    if (err) return false
    wx.showToast({
      title: '修改地址成功',
      icon: 'none'
    })
    wx.navigateBack({
      delta: 0
    })
    return true
  },

  submitAddress() {
    this.data.addressId ? this.updateAddress() : this.addAddress()
  },

  onChangeDetail(e) {
    const value = e.detail
    this.setData({
      'addressInfo.detail': value
    })
  },

  onChangeName(e) {
    const value = e.detail
    this.setData({
      'addressInfo.name': value
    })
  },

  onChangePhone(e) {
    const value = e.detail
    this.setData({
      'addressInfo.phone': value
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const { addressId } = options
    this.setData(
      {
        addressId
      },
      () => {
        if (this.data.addressId) {
          this.getAddressDetail()
        }
      }
    )
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {}
})
